﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Globalization;
using System.Resources;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Collections;

namespace etimet
{
    static class TranslationHelper
    {
        static private ResourceManager rm = null;
        
        static private CultureInfo default_ci = null;

        static private CultureInfo lang_ci = null;

        /// <summary>
        /// translate control into the specific lang, or leave it untranslated if no translation string found
        /// </summary>
        /// <param name="ctrl"></param>
        /// <param name="lang"></param>
        static private void Translate(Control ctrl, string lang)
        {
            string str = TranslateString(ctrl.Name, lang);
            if (str != null)
                ctrl.Text = str;
        }

        static private void Translate(ToolStripMenuItem o, string lang)
        {
            string str = TranslateString(o.Name, lang);
            if (str != null)
                o.Text = str;
        }

        static private void Translate(ToolStripItem o, string lang)
        {
            string str = TranslateString(o.Name, lang);
            if (str != null)
                o.Text = str;
        }

        static private void Translate(Form o, string lang)
        {
            string str = TranslateString(o.Name, lang);
            if (str != null)
                o.Text = str;
        }

        /// <summary>
        /// returns the <c>name</c> string from the <c>lang</c> resource
        /// </summary>
        /// <param name="name">string/key name</param>
        /// <param name="lang">the language to translate to</param>
        /// <returns>a translated string for <c>name</c></returns>
        static public string TranslateString(string name, string lang)
        {
            if (lang_ci == null || !lang_ci.TwoLetterISOLanguageName.Equals(lang))
                lang_ci = new CultureInfo(lang);

            try
            {
                return rm.GetString(name, lang_ci);
            }
            catch (Exception)
            {
                // no translation yet
                try
                {
                    return rm.GetString(name, default_ci);
                }
                catch (Exception)
                {
                    return null;
                    //MessageBox.Show(ex.Message.ToString(), name);
                    //System.Console.WriteLine(ex.Message.ToString());
                }
            }
        }

        static public void TranslateForm(string lang, Form parent)
        {
            if (rm == null)
                rm = new ResourceManager("etimet.i18nResources.i18n", Assembly.GetExecutingAssembly(), null);
            if (default_ci == null)
                default_ci = new CultureInfo("en");

            // handle direction
            if (Program.conf.CurrentLang.Equals("he") || Program.conf.CurrentLang.Equals("ar") || Program.conf.CurrentLang.Equals("fa"))
            {
                parent.RightToLeft = RightToLeft.Yes;
            }
            else
            {
                parent.RightToLeft = RightToLeft.No;
            }

            // translate the form itself
            Translate(parent, lang);

            // translate all the children controls, recursively
            Control.ControlCollection c = parent.Controls;
            foreach (Control o in c)
            {
                // special handling for the menu
                if (o.GetType() == typeof(MenuStrip))
                {
                    foreach (ToolStripMenuItem it in ((MenuStrip)o).Items)
                    {
                        Translate(it, lang);
                        //MessageBox.Show(it.Text);
                        foreach (ToolStripItem f in it.DropDownItems)
                        {
                            if (typeof(ToolStripSeparator) != f.GetType())
                                Translate(f, lang);
                        }
                    }
                }
                RecursiveTranslateCtrl(lang, o);
            }
        }

        static private void RecursiveTranslateCtrl(string lang, Control ctrl)
        {
            Translate(ctrl, lang);

            foreach (Control c in ctrl.Controls)
            {
                RecursiveTranslateCtrl(lang, c);
            }
        }
    }
}
